package com.wh.custom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.basic.utils.ExcelUtils;
import com.wh.custom.dao.*;
import com.wh.custom.pojo.CustomHighseascustom;
import com.wh.custom.pojo.CustomMycustom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.wh.custom.pojo.CustomSalesmancustom;
import com.wh.custom.service.ICustomSalesmancustomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wh.custom.mapper.CustomSalesmancustomMapper;
import com.wh.custom.dto.CustomSalesmancustomDto;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class CustomSalesmancustomServiceImpl extends ServiceImpl<CustomSalesmancustomMapper, CustomSalesmancustom> implements ICustomSalesmancustomService {

    @Autowired
    private CustomSalesmancustomMapper customSalesmancustomMapper;
    @Autowired
    private CustomHighSeasCustomMapper customHighSeasCustomMapper;
    @Autowired
    public CustomTypeMapper customTypeMapper;
    @Autowired
    public CustomLabelMapper customLabelMapper;
    @Autowired
    public CustomSourceMapper customSourceMapper;
    @Autowired
    public CustomSalesmanMapper customSalesmanMapper;

    @Override
    public void add(CustomSalesmancustom customSalesmancustom) {
        customSalesmancustomMapper.insert(customSalesmancustom);
    }

    @Override
    public void deleteById(Long id) {
        customSalesmancustomMapper.deleteById(id);
    }

    @Transactional
    @Override
    public void patchDel(List<Long> ids) {
        customSalesmancustomMapper.deleteBatchIds(ids);
    }



    @Override
    public CustomSalesmancustom findOne(Long id) {
        return customSalesmancustomMapper.selectById(id);
    }

    @Override
    public List<CustomSalesmancustom> findAll() {
        return customSalesmancustomMapper.selectList(null);
    }

    @Override
    public IPage<CustomSalesmancustom> findByPage(CustomSalesmancustomDto customSalesmancustomDto) {
        //1.创建查询条件
        QueryWrapper<CustomSalesmancustom> qw = new QueryWrapper<>();
        //qw.like("xxx",customSalesmancustomDto.getXxx());
        //qw.or();
        //qw.like("xxx",customSalesmancustomDto.getXxx());
        //2.组件分页数据
        IPage<CustomSalesmancustom> page = new Page<>(customSalesmancustomDto.getCurrentPage(), customSalesmancustomDto.getPageSize());
        page.setRecords(customSalesmancustomMapper.findByPage(page, qw));
        page.setTotal(customSalesmancustomMapper.selectCount(qw));
        //3.返回
        return page;
    }
    @Override
    public void export(HttpServletResponse response) {
        List<CustomSalesmancustom> mycustoms = customSalesmancustomMapper.selectList(null);
        //将关联表数据填充上去
        for (CustomSalesmancustom mycustom : mycustoms) {
            mycustom.setTypes(customTypeMapper.selectByIdname(mycustom.getTypeId()));
            mycustom.setLabels(customLabelMapper.selectByIdname(mycustom.getLabelId()));
            mycustom.setSources(customSourceMapper.selectByIdname(mycustom.getSourceId()));
            mycustom.setSalesmans(customSalesmanMapper.selectByIdname(mycustom.getSalesmanId()));
        }
        //ExcelUtils.exportExcel(导出的数据, 标题, 页名称, 导出的类型, 文件名, 响应对象)
        //文件名如果不写后缀默认是xls，可以写上xlsx就是指定的xlsx类型
        SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY_MM_dd");
        String time = dateFormat.format(new Date());
        ExcelUtils.exportExcel(mycustoms, "业务员客户信息表", "客户信息", CustomSalesmancustom.class, "业务员客户信息表"+ time +".xls", response);
    }

    public void recovery(List<Long> ids) {
        System.out.println(ids);
        CustomHighseascustom customHighseascustom = new CustomHighseascustom();
        for (Long id : ids) {
            CustomSalesmancustom mycustom = customSalesmancustomMapper.selectById(id);

            System.out.println(mycustom);
            customHighseascustom.setCompanyName(mycustom.getCompanyName());
            customHighseascustom.setCustomerName(mycustom.getCustomerName());

            customHighseascustom.setOrderProductCount(mycustom.getOrderProductCount());
            customHighseascustom.setTotalAmount(mycustom.getTotalAmount());
            customHighseascustom.setPhoneNumber(mycustom.getPhoneNumber());
            customHighseascustom.setAddress(mycustom.getAddress());
            customHighseascustom.setLastDealTime(mycustom.getLastDealTime());
            customHighseascustom.setUpdateTime(mycustom.getUpdateTime());
            customHighseascustom.setTypeId(mycustom.getTypeId());
            customHighseascustom.setLabelId(mycustom.getLabelId());
            ;
            customHighseascustom.setSourceId(mycustom.getSourceId());
            customHighseascustom.setSalesmanId(mycustom.getSalesmanId());
            customHighseascustom.setNotes(mycustom.getNotes());
            customHighSeasCustomMapper.insert(customHighseascustom);
            customSalesmancustomMapper.deleteById(id);
        }
    }
}